home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / iparam.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  32.7 KB  |  1,132 lines

  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: iparam.c,v 1.5 2000/09/19 19:00:46 lpd Exp $ */
  20. /* Interpreter implementations of parameter dictionaries */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "oper.h"        /* for check_type */
  26. #include "opcheck.h"
  27. #include "ialloc.h"
  28. #include "idict.h"
  29. #include "imemory.h"        /* for iutil.h */
  30. #include "iname.h"
  31. #include "istack.h"
  32. #include "iparam.h"
  33. #include "iutil.h"        /* for num_params */
  34. #include "ivmspace.h"
  35. #include "store.h"
  36.  
  37. /* ================ Utilities ================ */
  38.  
  39. /* Convert a key to a ref. */
  40. private int
  41. ref_param_key(const iparam_list * plist, gs_param_name pkey, ref * pkref)
  42. {
  43.     if (plist->int_keys) {
  44.     long key;
  45.  
  46.     if (sscanf(pkey, "%ld", &key) != 1)
  47.         return_error(e_rangecheck);
  48.     make_int(pkref, key);
  49.     return 0;
  50.     } else
  51.     return name_ref((const byte *)pkey, strlen(pkey), pkref, 0);
  52. }
  53.  
  54. /* Fill in a gs_param_key_t from a name or int ref. */
  55. private int
  56. ref_to_key(const ref * pref, gs_param_key_t * key, iparam_list *plist)
  57. {
  58.     if (r_has_type(pref, t_name)) {
  59.     ref nref;
  60.  
  61.     name_string_ref(pref, &nref);
  62.     key->data = nref.value.const_bytes;
  63.     key->size = r_size(&nref);
  64.     key->persistent = false; /* names may be freed */
  65.     } else if (r_has_type(pref, t_integer)) {
  66.     char istr[sizeof(long) * 8 / 3 + 2];
  67.     int len;
  68.     byte *buf;
  69.  
  70.     sprintf(istr, "%ld", pref->value.intval);
  71.     len = strlen(istr);
  72.     /* GC will take care of freeing this: */
  73.     buf = gs_alloc_string(plist->memory, len, "ref_to_key");
  74.     if (!buf)
  75.         return_error(e_VMerror);
  76.     key->data = buf;
  77.     key->size = len;
  78.     key->persistent = true;
  79.     } else
  80.     return_error(e_typecheck);
  81.     return 0;
  82. }
  83.  
  84. /* ================ Writing parameters to refs ================ */
  85.  
  86. /* Forward references */
  87. private int array_new_indexed_plist_write(P4(dict_param_list *plist,
  88.                          ref *parray, const ref *pwanted,
  89.                          gs_ref_memory_t *imem));
  90.  
  91. /* ---------------- Generic writing procedures ---------------- */
  92.  
  93. private param_proc_begin_xmit_collection(ref_param_begin_write_collection);
  94. private param_proc_end_xmit_collection(ref_param_end_write_collection);
  95. private param_proc_xmit_typed(ref_param_write_typed);
  96. private param_proc_next_key(ref_param_get_next_key);
  97. private param_proc_requested(ref_param_requested);
  98. private const gs_param_list_procs ref_write_procs =
  99. {
  100.     ref_param_write_typed,
  101.     ref_param_begin_write_collection,
  102.     ref_param_end_write_collection,
  103.     ref_param_get_next_key,
  104.     NULL,            /* request */
  105.     ref_param_requested
  106. };
  107. private int ref_array_param_requested(P5(const iparam_list *, gs_param_name,
  108.                      ref *, uint, client_name_t));
  109. private int ref_param_write(P3(iparam_list *, gs_param_name, const ref *));
  110. private int ref_param_write_string_value(P3(ref *, const gs_param_string *,
  111.                         gs_ref_memory_t *));
  112. private int ref_param_write_name_value(P2(ref *, const gs_param_string *));
  113. private int
  114. ref_param_make_int(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
  115. {
  116.     make_tav(pe, t_integer, imemory_new_mask(imem), intval,
  117.          ((const gs_param_int_array *)pvalue)->data[i]);
  118.     return 0;
  119. }
  120. private int
  121. ref_param_make_float(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
  122. {
  123.     make_tav(pe, t_real, imemory_new_mask(imem), realval,
  124.          ((const gs_param_float_array *)pvalue)->data[i]);
  125.     return 0;
  126. }
  127. private int
  128. ref_param_make_string(ref *pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
  129. {
  130.     return ref_param_write_string_value(pe,
  131.              &((const gs_param_string_array *)pvalue)->data[i],
  132.                     imem);
  133. }
  134. private int
  135. ref_param_make_name(ref * pe, const void *pvalue, uint i, gs_ref_memory_t *imem)
  136. {
  137.     return ref_param_write_name_value(pe,
  138.              &((const gs_param_string_array *)pvalue)->data[i]);
  139. }
  140. private int
  141. ref_param_write_typed_array(gs_param_list * plist, gs_param_name pkey,
  142.                 void *pvalue, uint count,
  143.                 int (*make)(P4(ref *, const void *, uint,
  144.                        gs_ref_memory_t *)))
  145. {
  146.     iparam_list *const iplist = (iparam_list *) plist;
  147.     ref value;
  148.     uint i;
  149.     ref *pe;
  150.     int code;
  151.  
  152.     if ((code = ref_array_param_requested(iplist, pkey, &value, count,
  153.                        "ref_param_write_typed_array")) <= 0)
  154.     return code;
  155.     for (i = 0, pe = value.value.refs; i < count; ++i, ++pe)
  156.     if ((code = (*make) (pe, pvalue, i, iplist->ref_memory)) < 0)
  157.         return code;
  158.     return ref_param_write(iplist, pkey, &value);
  159. }
  160. private int
  161. ref_param_begin_write_collection(gs_param_list * plist, gs_param_name pkey,
  162.                  gs_param_dict * pvalue,
  163.                  gs_param_collection_type_t coll_type)
  164. {
  165.     iparam_list *const iplist = (iparam_list *) plist;
  166.     gs_ref_memory_t *imem = iplist->ref_memory;
  167.     dict_param_list *dlist = (dict_param_list *)
  168.     gs_alloc_bytes(plist->memory, size_of(dict_param_list),
  169.                "ref_param_begin_write_collection");
  170.     int code;
  171.  
  172.     if (dlist == 0)
  173.     return_error(e_VMerror);
  174.     if (coll_type != gs_param_collection_array) {
  175.     ref dref;
  176.  
  177.     code = dict_alloc(imem, pvalue->size, &dref);
  178.     if (code >= 0) {
  179.         code = dict_param_list_write(dlist, &dref, NULL, imem);
  180.         dlist->int_keys = coll_type == gs_param_collection_dict_int_keys;
  181.     }
  182.     } else {
  183.     ref aref;
  184.  
  185.     code = gs_alloc_ref_array(imem, &aref, a_all, pvalue->size,
  186.                   "ref_param_begin_write_collection");
  187.     if (code >= 0)
  188.         code = array_new_indexed_plist_write(dlist, &aref, NULL, imem);
  189.     }
  190.     if (code < 0)
  191.     gs_free_object(plist->memory, dlist, "ref_param_begin_write_collection");
  192.     else
  193.     pvalue->list = (gs_param_list *) dlist;
  194.     return code;
  195. }
  196. private int
  197. ref_param_end_write_collection(gs_param_list * plist, gs_param_name pkey,
  198.                    gs_param_dict * pvalue)
  199. {
  200.     iparam_list *const iplist = (iparam_list *) plist;
  201.     int code = ref_param_write(iplist, pkey,
  202.                    &((dict_param_list *) pvalue->list)->dict);
  203.  
  204.     gs_free_object(plist->memory, pvalue->list, "ref_param_end_write_collection");
  205.     return code;
  206. }
  207. private int
  208. ref_param_write_typed(gs_param_list * plist, gs_param_name pkey,
  209.               gs_param_typed_value * pvalue)
  210. {
  211.     iparam_list *const iplist = (iparam_list *) plist;
  212.     ref value;
  213.     int code = 0;
  214.  
  215.     switch (pvalue->type) {
  216.     case gs_param_type_null:
  217.         make_null(&value);
  218.         break;
  219.     case gs_param_type_bool:
  220.         make_bool(&value, pvalue->value.b);
  221.         break;
  222.     case gs_param_type_int:
  223.         make_int(&value, pvalue->value.i);
  224.         break;
  225.     case gs_param_type_long:
  226.         make_int(&value, pvalue->value.l);
  227.         break;
  228.     case gs_param_type_float:
  229.         make_real(&value, pvalue->value.f);
  230.         break;
  231.     case gs_param_type_string:
  232.         if (!ref_param_requested(plist, pkey))
  233.         return 0;
  234.         code = ref_param_write_string_value(&value, &pvalue->value.s,
  235.                         iplist->ref_memory);
  236.         break;
  237.     case gs_param_type_name:
  238.         if (!ref_param_requested(plist, pkey))
  239.         return 0;
  240.         code = ref_param_write_name_value(&value, &pvalue->value.n);
  241.         break;
  242.     case gs_param_type_int_array:
  243.         return ref_param_write_typed_array(plist, pkey, &pvalue->value.ia,
  244.                            pvalue->value.ia.size,
  245.                            ref_param_make_int);
  246.     case gs_param_type_float_array:
  247.         return ref_param_write_typed_array(plist, pkey, &pvalue->value.fa,
  248.                            pvalue->value.fa.size,
  249.                            ref_param_make_float);
  250.     case gs_param_type_string_array:
  251.         return ref_param_write_typed_array(plist, pkey, &pvalue->value.sa,
  252.                            pvalue->value.sa.size,
  253.                            ref_param_make_string);
  254.     case gs_param_type_name_array:
  255.         return ref_param_write_typed_array(plist, pkey, &pvalue->value.na,
  256.                            pvalue->value.na.size,
  257.                            ref_param_make_name);
  258.     case gs_param_type_dict:
  259.     case gs_param_type_dict_int_keys:
  260.     case gs_param_type_array:
  261.         return ref_param_begin_write_collection(plist, pkey,
  262.                             &pvalue->value.d,
  263.           (gs_param_collection_type_t)(pvalue->type - gs_param_type_dict));
  264.     default:
  265.         return_error(e_typecheck);
  266.     }
  267.     if (code < 0)
  268.     return code;
  269.     return ref_param_write(iplist, pkey, &value);
  270. }
  271.  
  272. /* Check whether a given parameter was requested. */
  273. private int
  274. ref_param_requested(const gs_param_list * plist, gs_param_name pkey)
  275. {
  276.     const iparam_list *const ciplist = (const iparam_list *)plist;
  277.     ref kref;
  278.     ref *ignore_value;
  279.  
  280.     if (!r_has_type(&ciplist->u.w.wanted, t_dictionary))
  281.     return -1;
  282.     if (ref_param_key(ciplist, pkey, &kref) < 0)
  283.     return -1;        /* catch it later */
  284.     return (dict_find(&ciplist->u.w.wanted, &kref, &ignore_value) > 0);
  285. }
  286.  
  287. /* Check whether an array parameter is wanted, and allocate it if so. */
  288. /* Return <0 on error, 0 if not wanted, 1 if wanted. */
  289. private int
  290. ref_array_param_requested(const iparam_list *iplist, gs_param_name pkey,
  291.               ref *pvalue, uint size, client_name_t cname)
  292. {
  293.     int code;
  294.  
  295.     if (!ref_param_requested((const gs_param_list *)iplist, pkey))
  296.     return 0;
  297.     code = gs_alloc_ref_array(iplist->ref_memory, pvalue, a_all, size, cname);
  298.     return (code < 0 ? code : 1);
  299. }
  300.  
  301. /* ---------------- Internal routines ---------------- */
  302.  
  303. /* Prepare to write a string value. */
  304. private int
  305. ref_param_write_string_value(ref * pref, const gs_param_string * pvalue,
  306.                  gs_ref_memory_t *imem)
  307. {
  308.     const byte *pdata = pvalue->data;
  309.     uint n = pvalue->size;
  310.  
  311.     if (pvalue->persistent)
  312.     make_const_string(pref, a_readonly | avm_foreign, n, pdata);
  313.     else {
  314.     byte *pstr = gs_alloc_string((gs_memory_t *)imem, n,
  315.                      "ref_param_write_string");
  316.  
  317.     if (pstr == 0)
  318.         return_error(e_VMerror);
  319.     memcpy(pstr, pdata, n);
  320.     make_string(pref, a_readonly | imemory_space(imem), n, pstr);
  321.     }
  322.     return 0;
  323. }
  324.  
  325. /* Prepare to write a name value. */
  326. private int
  327. ref_param_write_name_value(ref * pref, const gs_param_string * pvalue)
  328. {
  329.     return name_ref(pvalue->data, pvalue->size, pref,
  330.             (pvalue->persistent ? 0 : 1));
  331. }
  332.  
  333. /* Generic routine for writing a ref parameter. */
  334. private int
  335. ref_param_write(iparam_list * plist, gs_param_name pkey, const ref * pvalue)
  336. {
  337.     ref kref;
  338.     int code;
  339.  
  340.     if (!ref_param_requested((gs_param_list *) plist, pkey))
  341.     return 0;
  342.     code = ref_param_key(plist, pkey, &kref);
  343.     if (code < 0)
  344.     return code;
  345.     return (*plist->u.w.write) (plist, &kref, pvalue);
  346. }
  347.  
  348. /* ---------------- Implementations ---------------- */
  349.  
  350. /* Initialize for writing parameters. */
  351. private void
  352. ref_param_write_init(iparam_list * plist, const ref * pwanted,
  353.              gs_ref_memory_t *imem)
  354. {
  355.     gs_param_list_init((gs_param_list *)plist, &ref_write_procs,
  356.                (gs_memory_t *)imem);
  357.     plist->ref_memory = imem;
  358.     if (pwanted == 0)
  359.     make_null(&plist->u.w.wanted);
  360.     else
  361.     plist->u.w.wanted = *pwanted;
  362.     plist->results = 0;
  363.     plist->int_keys = false;
  364. }
  365.  
  366. /* Implementation for getting parameters to a stack. */
  367. private int
  368. stack_param_write(iparam_list * plist, const ref * pkey, const ref * pvalue)
  369. {
  370.     stack_param_list *const splist = (stack_param_list *) plist;
  371.     ref_stack_t *pstack = splist->pstack;
  372.     s_ptr p = pstack->p;
  373.  
  374.     if (pstack->top - p < 2) {
  375.     int code = ref_stack_push(pstack, 2);
  376.  
  377.     if (code < 0)
  378.         return code;
  379.     *ref_stack_index(pstack, 1) = *pkey;
  380.     p = pstack->p;
  381.     } else {
  382.     pstack->p = p += 2;
  383.     p[-1] = *pkey;
  384.     }
  385.     *p = *pvalue;
  386.     splist->count++;
  387.     return 0;
  388. }
  389.  
  390. /* Implementation for enumerating parameters on a stack */
  391. private int            /* ret 0 ok, 1 if EOF, or -ve err */
  392. stack_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
  393.               gs_param_key_t * key, ref_type * type)
  394. {
  395.     int code;
  396.     stack_param_list *const splist = (stack_param_list *) plist;
  397.     long index = penum->intval;
  398.     ref *stack_element;
  399.  
  400.     do {
  401.     stack_element =
  402.         ref_stack_index(splist->pstack, index + 1 + splist->skip);
  403.     if (!stack_element)
  404.         return 1;
  405.     } while (index += 2, !r_has_type(stack_element, t_name));
  406.     *type = r_type(stack_element);
  407.     code = ref_to_key(stack_element, key, plist);
  408.     penum->intval = index;
  409.     return code;
  410. }
  411.  
  412. int
  413. stack_param_list_write(stack_param_list * plist, ref_stack_t * pstack,
  414.                const ref * pwanted, gs_ref_memory_t *imem)
  415. {
  416.     plist->u.w.write = stack_param_write;
  417.     ref_param_write_init((iparam_list *) plist, pwanted, imem);
  418.     plist->enumerate = stack_param_enumerate;
  419.     plist->pstack = pstack;
  420.     plist->skip = 0;
  421.     plist->count = 0;
  422.     return 0;
  423. }
  424.  
  425. /* Implementation for getting parameters to a dictionary. */
  426. private int
  427. dict_param_write(iparam_list * plist, const ref * pkey, const ref * pvalue)
  428. {
  429.     int code =
  430.     dict_put(&((dict_param_list *) plist)->dict, pkey, pvalue, NULL);
  431.  
  432.     return min(code, 0);
  433. }
  434.  
  435. /* Implementation for enumerating parameters in a dictionary */
  436. private int            /* ret 0 ok, 1 if EOF, or -ve err */
  437. dict_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
  438.              gs_param_key_t * key, ref_type * type)
  439. {
  440.     ref elt[2];
  441.     int code;
  442.     dict_param_list *const pdlist = (dict_param_list *) plist;
  443.     int index =
  444.     (penum->intval != 0 ? penum->intval : dict_first(&pdlist->dict));
  445.  
  446.     index = dict_next(&pdlist->dict, index, elt);
  447.     if (index < 0)
  448.     return 1;
  449.     *type = r_type(&elt[1]);
  450.     code = ref_to_key(&elt[0], key, plist);
  451.     penum->intval = index;
  452.     return code;
  453. }
  454.  
  455. int
  456. dict_param_list_write(dict_param_list *plist, ref *pdict, const ref *pwanted,
  457.               gs_ref_memory_t *imem)
  458. {
  459.     check_dict_write(*pdict);
  460.     plist->u.w.write = dict_param_write;
  461.     plist->enumerate = dict_param_enumerate;
  462.     ref_param_write_init((iparam_list *) plist, pwanted, imem);
  463.     plist->dict = *pdict;
  464.     return 0;
  465. }
  466.  
  467. /* Implementation for getting parameters to an indexed array. */
  468. /* Note that this is now internal, since it only handles "new" arrays. */
  469. private int
  470. array_new_indexed_param_write(iparam_list * iplist, const ref * pkey,
  471.               const ref * pvalue)
  472. {
  473.     const ref *const arr = &((dict_param_list *)iplist)->dict;
  474.     ref *eltp;
  475.  
  476.     if (!r_has_type(pkey, t_integer))
  477.     return_error(e_typecheck);
  478.     check_int_ltu(*pkey, r_size(arr));
  479.     store_check_dest(arr, pvalue);
  480.     eltp = arr->value.refs + pkey->value.intval;
  481.     /* ref_assign_new(eltp, pvalue); */
  482.     ref_assign(eltp, pvalue);
  483.     r_set_attrs(eltp, imemory_new_mask(iplist->ref_memory));
  484.     return 0;
  485. }
  486. private int
  487. array_new_indexed_plist_write(dict_param_list * plist, ref * parray,
  488.                   const ref * pwanted, gs_ref_memory_t *imem)
  489. {
  490.     check_array(*parray);
  491.     check_write(*parray);
  492.     plist->u.w.write = array_new_indexed_param_write;
  493.     ref_param_write_init((iparam_list *) plist, pwanted, imem);
  494.     plist->dict = *parray;
  495.     plist->int_keys = true;
  496.     return 0;
  497. }
  498.  
  499. /* ================ Reading refs to parameters ================ */
  500.  
  501. /* ---------------- Generic reading procedures ---------------- */
  502.  
  503. private param_proc_begin_xmit_collection(ref_param_begin_read_collection);
  504. private param_proc_end_xmit_collection(ref_param_end_read_collection);
  505. private param_proc_xmit_typed(ref_param_read_typed);
  506.  
  507. /*private param_proc_next_key(ref_param_get_next_key); already dec'ld above */
  508. private param_proc_get_policy(ref_param_read_get_policy);
  509. private param_proc_signal_error(ref_param_read_signal_error);
  510. private param_proc_commit(ref_param_read_commit);
  511. private const gs_param_list_procs ref_read_procs =
  512. {
  513.     ref_param_read_typed,
  514.     ref_param_begin_read_collection,
  515.     ref_param_end_read_collection,
  516.     ref_param_get_next_key,
  517.     NULL,            /* request */
  518.     NULL,            /* requested */
  519.     ref_param_read_get_policy,
  520.     ref_param_read_signal_error,
  521.     ref_param_read_commit
  522. };
  523. private int ref_param_read(P4(iparam_list *, gs_param_name,
  524.                   iparam_loc *, int));
  525. private int ref_param_read_string_value(P2(const iparam_loc *,
  526.                        gs_param_string *));
  527. private int ref_param_read_array(P3(iparam_list *, gs_param_name,
  528.                     iparam_loc *));
  529.  
  530. #define iparam_note_error(loc, code)\
  531.   gs_note_error(*(loc).presult = code)
  532. #define iparam_check_type(loc, typ)\
  533.   if ( !r_has_type((loc).pvalue, typ) )\
  534.     return iparam_note_error(loc, e_typecheck)
  535. #define iparam_check_read(loc)\
  536.   if ( !r_has_attr((loc).pvalue, a_read) )\
  537.     return iparam_note_error(loc, e_invalidaccess)
  538.  
  539. private int
  540. ref_param_read_int_array(gs_param_list * plist, gs_param_name pkey,
  541.              gs_param_int_array * pvalue)
  542. {
  543.     iparam_list *const iplist = (iparam_list *) plist;
  544.     iparam_loc loc;
  545.     int code = ref_param_read_array(iplist, pkey, &loc);
  546.     int *piv;
  547.     uint size;
  548.     long i;
  549.  
  550.     if (code != 0)
  551.     return code;
  552.     size = r_size(loc.pvalue);
  553.     piv = (int *)gs_alloc_byte_array(plist->memory, size, sizeof(int),
  554.                      "ref_param_read_int_array");
  555.  
  556.     if (piv == 0)
  557.     return_error(e_VMerror);
  558.     for (i = 0; i < size; i++) {
  559.     ref elt;
  560.  
  561.     array_get(loc.pvalue, i, &elt);
  562.     if (!r_has_type(&elt, t_integer)) {
  563.         code = gs_note_error(e_typecheck);
  564.         break;
  565.     }
  566. #if arch_sizeof_int < arch_sizeof_long
  567.     if (elt.value.intval != (int)elt.value.intval) {
  568.         code = gs_note_error(e_rangecheck);
  569.         break;
  570.     }
  571. #endif
  572.     piv[i] = (int)elt.value.intval;
  573.     }
  574.     if (code < 0) {
  575.     gs_free_object(plist->memory, piv, "ref_param_read_int_array");
  576.     return (*loc.presult = code);
  577.     }
  578.     pvalue->data = piv;
  579.     pvalue->size = size;
  580.     pvalue->persistent = true;
  581.     return 0;
  582. }
  583. private int
  584. ref_param_read_float_array(gs_param_list * plist, gs_param_name pkey,
  585.                gs_param_float_array * pvalue)
  586. {
  587.     iparam_list *const iplist = (iparam_list *) plist;
  588.     iparam_loc loc;
  589.     ref aref, elt;
  590.     int code = ref_param_read_array(iplist, pkey, &loc);
  591.     float *pfv;
  592.     uint size;
  593.     long i;
  594.  
  595.     if (code != 0)
  596.     return code;
  597.     size = r_size(loc.pvalue);
  598.     pfv = (float *)gs_alloc_byte_array(plist->memory, size, sizeof(float),
  599.                        "ref_param_read_float_array");
  600.  
  601.     if (pfv == 0)
  602.     return_error(e_VMerror);
  603.     aref = *loc.pvalue;
  604.     loc.pvalue = &elt;
  605.     for (i = 0; code >= 0 && i < size; i++) {
  606.     array_get(&aref, i, &elt);
  607.     code = float_param(&elt, pfv + i);
  608.     }
  609.     if (code < 0) {
  610.     gs_free_object(plist->memory, pfv, "ref_read_float_array_param");
  611.     return (*loc.presult = code);
  612.     }
  613.     pvalue->data = pfv;
  614.     pvalue->size = size;
  615.     pvalue->persistent = true;
  616.     return 0;
  617. }
  618. private int
  619. ref_param_read_string_array(gs_param_list * plist, gs_param_name pkey,
  620.                 gs_param_string_array * pvalue)
  621. {
  622.     iparam_list *const iplist = (iparam_list *) plist;
  623.     iparam_loc loc;
  624.     ref aref;
  625.     int code = ref_param_read_array(iplist, pkey, &loc);
  626.     gs_param_string *psv;
  627.     uint size;
  628.     long i;
  629.  
  630.     if (code != 0)
  631.     return code;
  632.     size = r_size(loc.pvalue);
  633.     psv = (gs_param_string *)
  634.     gs_alloc_byte_array(plist->memory, size, sizeof(gs_param_string),
  635.                 "ref_param_read_string_array");
  636.     if (psv == 0)
  637.     return_error(e_VMerror);
  638.     aref = *loc.pvalue;
  639.     if (r_has_type(&aref, t_array)) {
  640.     for (i = 0; code >= 0 && i < size; i++) {
  641.         loc.pvalue = aref.value.refs + i;
  642.         code = ref_param_read_string_value(&loc, psv + i);
  643.     }
  644.     } else {
  645.     ref elt;
  646.  
  647.     loc.pvalue = &elt;
  648.     for (i = 0; code >= 0 && i < size; i++) {
  649.         array_get(&aref, i, &elt);
  650.         code = ref_param_read_string_value(&loc, psv + i);
  651.     }
  652.     }
  653.     if (code < 0) {
  654.     gs_free_object(plist->memory, psv, "ref_param_read_string_array");
  655.     return (*loc.presult = code);
  656.     }
  657.     pvalue->data = psv;
  658.     pvalue->size = size;
  659.     pvalue->persistent = true;
  660.     return 0;
  661. }
  662. private int
  663. ref_param_begin_read_collection(gs_param_list * plist, gs_param_name pkey,
  664.                 gs_param_dict * pvalue,
  665.                 gs_param_collection_type_t coll_type)
  666. {
  667.     iparam_list *const iplist = (iparam_list *) plist;
  668.     iparam_loc loc;
  669.     bool int_keys = coll_type != 0;
  670.     int code = ref_param_read(iplist, pkey, &loc, -1);
  671.     dict_param_list *dlist;
  672.  
  673.     if (code != 0)
  674.     return code;
  675.     dlist = (dict_param_list *)
  676.     gs_alloc_bytes(plist->memory, size_of(dict_param_list),
  677.                "ref_param_begin_read_collection");
  678.     if (dlist == 0)
  679.     return_error(e_VMerror);
  680.     if (r_has_type(loc.pvalue, t_dictionary)) {
  681.     code = dict_param_list_read(dlist, loc.pvalue, NULL, false,
  682.                     iplist->ref_memory);
  683.     dlist->int_keys = int_keys;
  684.     if (code >= 0)
  685.         pvalue->size = dict_length(loc.pvalue);
  686.     } else if (int_keys && r_is_array(loc.pvalue)) {
  687.     code = array_indexed_param_list_read(dlist, loc.pvalue, NULL, false,
  688.                          iplist->ref_memory);
  689.     if (code >= 0)
  690.         pvalue->size = r_size(loc.pvalue);
  691.     } else
  692.     code = gs_note_error(e_typecheck);
  693.     if (code < 0) {
  694.     gs_free_object(plist->memory, dlist, "ref_param_begin_write_collection");
  695.     return iparam_note_error(loc, code);
  696.     }
  697.     pvalue->list = (gs_param_list *) dlist;
  698.     return 0;
  699. }
  700. private int
  701. ref_param_end_read_collection(gs_param_list * plist, gs_param_name pkey,
  702.                   gs_param_dict * pvalue)
  703. {
  704.     iparam_list_release((dict_param_list *) pvalue->list);
  705.     gs_free_object(plist->memory, pvalue->list,
  706.            "ref_param_end_read_collection");
  707.     return 0;
  708. }
  709. private int
  710. ref_param_read_typed(gs_param_list * plist, gs_param_name pkey,
  711.              gs_param_typed_value * pvalue)
  712. {
  713.     iparam_list *const iplist = (iparam_list *) plist;
  714.     iparam_loc loc;
  715.     ref elt;
  716.     int code = ref_param_read(iplist, pkey, &loc, -1);
  717.  
  718.     if (code != 0)
  719.     return code;
  720.     switch (r_type(loc.pvalue)) {
  721.     case t_array:
  722.     case t_mixedarray:
  723.     case t_shortarray:
  724.         iparam_check_read(loc);
  725.         if (r_size(loc.pvalue) <= 0) {
  726.         /* 0-length array; can't get type info */
  727.         pvalue->type = gs_param_type_array;
  728.         pvalue->value.d.list = 0;
  729.         pvalue->value.d.size = 0;
  730.         return 0;
  731.         }
  732.         /*
  733.          * We have to guess at the array type.  First we guess based
  734.          * on the type of the first element of the array.  If that
  735.          * fails, we try again with more general types.
  736.          */
  737.         array_get(loc.pvalue, 0, &elt);
  738.         switch (r_type(&elt)) {
  739.         case t_integer:
  740.             pvalue->type = gs_param_type_int_array;
  741.             code = ref_param_read_int_array(plist, pkey,
  742.                             &pvalue->value.ia);
  743.             if (code != e_typecheck)
  744.             return code;
  745.             /* This might be a float array.  Fall through. */
  746.             *loc.presult = 0;  /* reset error */
  747.         case t_real:
  748.             pvalue->type = gs_param_type_float_array;
  749.             return ref_param_read_float_array(plist, pkey,
  750.                               &pvalue->value.fa);
  751.         case t_string:
  752.             pvalue->type = gs_param_type_string_array;
  753.             return ref_param_read_string_array(plist, pkey,
  754.                                &pvalue->value.sa);
  755.         case t_name:
  756.             pvalue->type = gs_param_type_name_array;
  757.             return ref_param_read_string_array(plist, pkey,
  758.                                &pvalue->value.na);
  759.         default:
  760.             break;
  761.         }
  762.         return gs_note_error(e_typecheck);
  763.     case t_boolean:
  764.         pvalue->type = gs_param_type_bool;
  765.         pvalue->value.b = loc.pvalue->value.boolval;
  766.         return 0;
  767.     case t_dictionary:
  768.         code = ref_param_begin_read_collection(plist, pkey,
  769.                 &pvalue->value.d, gs_param_collection_dict_any);
  770.         if (code < 0)
  771.         return code;
  772.         pvalue->type = gs_param_type_dict;
  773.  
  774.         /* fixup new dict's type & int_keys field if contents have int keys */
  775.         {
  776.         gs_param_enumerator_t enumr;
  777.         gs_param_key_t key;
  778.         ref_type keytype;
  779.  
  780.         param_init_enumerator(&enumr);
  781.         if (!(*((iparam_list *) plist)->enumerate)
  782.             ((iparam_list *) pvalue->value.d.list, &enumr, &key, &keytype)
  783.             && keytype == t_integer) {
  784.             ((dict_param_list *) pvalue->value.d.list)->int_keys = 1;
  785.             pvalue->type = gs_param_type_dict_int_keys;
  786.         }
  787.         }
  788.         return 0;
  789.     case t_integer:
  790.         pvalue->type = gs_param_type_long;
  791.         pvalue->value.l = loc.pvalue->value.intval;
  792.         return 0;
  793.     case t_name:
  794.         pvalue->type = gs_param_type_name;
  795.         return ref_param_read_string_value(&loc, &pvalue->value.n);
  796.     case t_null:
  797.         pvalue->type = gs_param_type_null;
  798.         return 0;
  799.     case t_real:
  800.         pvalue->value.f = loc.pvalue->value.realval;
  801.         pvalue->type = gs_param_type_float;
  802.         return 0;
  803.     case t_string:
  804.         pvalue->type = gs_param_type_string;
  805.         return ref_param_read_string_value(&loc, &pvalue->value.s);
  806.     default:
  807.         break;
  808.     }
  809.     return gs_note_error(e_typecheck);
  810. }
  811.  
  812. private int
  813. ref_param_read_get_policy(gs_param_list * plist, gs_param_name pkey)
  814. {
  815.     iparam_list *const iplist = (iparam_list *) plist;
  816.     ref *pvalue;
  817.  
  818.     if (!(r_has_type(&iplist->u.r.policies, t_dictionary) &&
  819.       dict_find_string(&iplist->u.r.policies, pkey, &pvalue) > 0 &&
  820.       r_has_type(pvalue, t_integer))
  821.     )
  822.     return gs_param_policy_ignore;
  823.     return (int)pvalue->value.intval;
  824. }
  825. private int
  826. ref_param_read_signal_error(gs_param_list * plist, gs_param_name pkey, int code)
  827. {
  828.     iparam_list *const iplist = (iparam_list *) plist;
  829.     iparam_loc loc;
  830.  
  831.     ref_param_read(iplist, pkey, &loc, -1);    /* can't fail */
  832.     *loc.presult = code;
  833.     switch (ref_param_read_get_policy(plist, pkey)) {
  834.     case gs_param_policy_ignore:
  835.         return 0;
  836.     case gs_param_policy_consult_user:
  837.         return_error(e_configurationerror);
  838.     default:
  839.         return code;
  840.     }
  841. }
  842. private int
  843. ref_param_read_commit(gs_param_list * plist)
  844. {
  845.     iparam_list *const iplist = (iparam_list *) plist;
  846.     int i;
  847.     int ecode = 0;
  848.  
  849.     if (!iplist->u.r.require_all)
  850.     return 0;
  851.     /* Check to make sure that all parameters were actually read. */
  852.     for (i = 0; i < iplist->count; ++i)
  853.     if (iplist->results[i] == 0)
  854.         iplist->results[i] = ecode = gs_note_error(e_undefined);
  855.     return ecode;
  856. }
  857. private int
  858. ref_param_get_next_key(gs_param_list * plist, gs_param_enumerator_t * penum,
  859.                gs_param_key_t * key)
  860. {
  861.     ref_type keytype;        /* result not needed here */
  862.     iparam_list *const pilist = (iparam_list *) plist;
  863.  
  864.     return (*pilist->enumerate) (pilist, penum, key, &keytype);
  865. }
  866.  
  867. /* ---------------- Internal routines ---------------- */
  868.  
  869. /* Read a string value. */
  870. private int
  871. ref_param_read_string_value(const iparam_loc * ploc, gs_param_string * pvalue)
  872. {
  873.     const ref *pref = ploc->pvalue;
  874.  
  875.     switch (r_type(pref)) {
  876.     case t_name: {
  877.         ref nref;
  878.  
  879.         name_string_ref(pref, &nref);
  880.         pvalue->data = nref.value.const_bytes;
  881.         pvalue->size = r_size(&nref);
  882.         pvalue->persistent = true;
  883.     }
  884.         break;
  885.     case t_string:
  886.         iparam_check_read(*ploc);
  887.         pvalue->data = pref->value.const_bytes;
  888.         pvalue->size = r_size(pref);
  889.         pvalue->persistent = false;
  890.         break;
  891.     default:
  892.         return iparam_note_error(*ploc, e_typecheck);
  893.     }
  894.     return 0;
  895. }
  896.  
  897. /* Read an array (or packed array) parameter. */
  898. private int
  899. ref_param_read_array(iparam_list * plist, gs_param_name pkey, iparam_loc * ploc)
  900. {
  901.     int code = ref_param_read(plist, pkey, ploc, -1);
  902.  
  903.     if (code != 0)
  904.     return code;
  905.     if (!r_is_array(ploc->pvalue))
  906.     return iparam_note_error(*ploc, e_typecheck);
  907.     iparam_check_read(*ploc);
  908.     return 0;
  909. }
  910.  
  911. /* Generic routine for reading a ref parameter. */
  912. private int
  913. ref_param_read(iparam_list * plist, gs_param_name pkey, iparam_loc * ploc,
  914.            int type)
  915. {
  916.     iparam_list *const iplist = (iparam_list *) plist;
  917.     ref kref;
  918.     int code = ref_param_key(plist, pkey, &kref);
  919.  
  920.     if (code < 0)
  921.     return code;
  922.     code = (*plist->u.r.read) (iplist, &kref, ploc);
  923.     if (code != 0)
  924.     return code;
  925.     if (type >= 0)
  926.     iparam_check_type(*ploc, type);
  927.     return 0;
  928. }
  929.  
  930. /* ---------------- Implementations ---------------- */
  931.  
  932. /* Implementation for putting parameters from an empty collection. */
  933. private int
  934. empty_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
  935. {
  936.     return 1;
  937. }
  938.  
  939. /* Initialize for reading parameters. */
  940. private int
  941. ref_param_read_init(iparam_list * plist, uint count, const ref * ppolicies,
  942.             bool require_all, gs_ref_memory_t *imem)
  943. {
  944.     gs_param_list_init((gs_param_list *)plist, &ref_read_procs,
  945.                (gs_memory_t *)imem);
  946.     plist->ref_memory = imem;
  947.     if (ppolicies == 0)
  948.     make_null(&plist->u.r.policies);
  949.     else
  950.     plist->u.r.policies = *ppolicies;
  951.     plist->u.r.require_all = require_all;
  952.     plist->count = count;
  953.     plist->results = (int *)
  954.     gs_alloc_byte_array(plist->memory, count, sizeof(int),
  955.                 "ref_param_read_init");
  956.  
  957.     if (plist->results == 0)
  958.     return_error(e_VMerror);
  959.     memset(plist->results, 0, count * sizeof(int));
  960.  
  961.     plist->int_keys = false;
  962.     return 0;
  963. }
  964.  
  965. /* Implementation for putting parameters from an indexed array. */
  966. private int
  967. array_indexed_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
  968. {
  969.     ref *const arr = &((dict_param_list *) plist)->dict;
  970.  
  971.     check_type(*pkey, t_integer);
  972.     if (pkey->value.intval < 0 || pkey->value.intval >= r_size(arr))
  973.     return 1;
  974.     ploc->pvalue = arr->value.refs + pkey->value.intval;
  975.     ploc->presult = &plist->results[pkey->value.intval];
  976.     *ploc->presult = 1;
  977.     return 0;
  978. }
  979. int
  980. array_indexed_param_list_read(dict_param_list * plist, const ref * parray,
  981.                   const ref * ppolicies, bool require_all,
  982.                   gs_ref_memory_t *ref_memory)
  983. {
  984.     iparam_list *const iplist = (iparam_list *) plist;
  985.     int code;
  986.  
  987.     check_read_type(*parray, t_array);
  988.     plist->u.r.read = array_indexed_param_read;
  989.     plist->dict = *parray;
  990.     code = ref_param_read_init(iplist, r_size(parray), ppolicies,
  991.                    require_all, ref_memory);
  992.     plist->int_keys = true;
  993.     return code;
  994. }
  995.  
  996. /* Implementation for putting parameters from an array. */
  997. private int
  998. array_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
  999. {
  1000.     ref *bot = ((array_param_list *) plist)->bot;
  1001.     ref *ptr = bot;
  1002.     ref *top = ((array_param_list *) plist)->top;
  1003.  
  1004.     for (; ptr < top; ptr += 2) {
  1005.     if (r_has_type(ptr, t_name) && name_eq(ptr, pkey)) {
  1006.         ploc->pvalue = ptr + 1;
  1007.         ploc->presult = &plist->results[ptr - bot];
  1008.         *ploc->presult = 1;
  1009.         return 0;
  1010.     }
  1011.     }
  1012.     return 1;
  1013. }
  1014.  
  1015. /* Implementation for enumerating parameters in an array */
  1016. private int            /* ret 0 ok, 1 if EOF, or -ve err */
  1017. array_param_enumerate(iparam_list * plist, gs_param_enumerator_t * penum,
  1018.               gs_param_key_t * key, ref_type * type)
  1019. {
  1020.     int index = penum->intval;
  1021.     ref *bot = ((array_param_list *) plist)->bot;
  1022.     ref *ptr = bot + index;
  1023.     ref *top = ((array_param_list *) plist)->top;
  1024.  
  1025.     for (; ptr < top; ptr += 2) {
  1026.     index += 2;
  1027.  
  1028.     if (r_has_type(ptr, t_name)) {
  1029.         int code = ref_to_key(ptr, key, plist);
  1030.  
  1031.         *type = r_type(ptr);
  1032.         penum->intval = index;
  1033.         return code;
  1034.     }
  1035.     }
  1036.     return 1;
  1037. }
  1038.  
  1039. int
  1040. array_param_list_read(array_param_list * plist, ref * bot, uint count,
  1041.               const ref * ppolicies, bool require_all,
  1042.               gs_ref_memory_t *imem)
  1043. {
  1044.     iparam_list *const iplist = (iparam_list *) plist;
  1045.  
  1046.     if (count & 1)
  1047.     return_error(e_rangecheck);
  1048.     plist->u.r.read = array_param_read;
  1049.     plist->enumerate = array_param_enumerate;
  1050.     plist->bot = bot;
  1051.     plist->top = bot + count;
  1052.     return ref_param_read_init(iplist, count, ppolicies, require_all, imem);
  1053. }
  1054.  
  1055. /* Implementation for putting parameters from a stack. */
  1056. private int
  1057. stack_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
  1058. {
  1059.     stack_param_list *const splist = (stack_param_list *) plist;
  1060.     ref_stack_t *pstack = splist->pstack;
  1061.  
  1062.     /* This implementation is slow, but it probably doesn't matter. */
  1063.     uint index = splist->skip + 1;
  1064.     uint count = splist->count;
  1065.  
  1066.     for (; count; count--, index += 2) {
  1067.     const ref *p = ref_stack_index(pstack, index);
  1068.  
  1069.     if (r_has_type(p, t_name) && name_eq(p, pkey)) {
  1070.         ploc->pvalue = ref_stack_index(pstack, index - 1);
  1071.         ploc->presult = &plist->results[count - 1];
  1072.         *ploc->presult = 1;
  1073.         return 0;
  1074.     }
  1075.     }
  1076.     return 1;
  1077. }
  1078. int
  1079. stack_param_list_read(stack_param_list * plist, ref_stack_t * pstack,
  1080.               uint skip, const ref * ppolicies, bool require_all,
  1081.               gs_ref_memory_t *imem)
  1082. {
  1083.     iparam_list *const iplist = (iparam_list *) plist;
  1084.     uint count = ref_stack_counttomark(pstack);
  1085.  
  1086.     if (count == 0)
  1087.     return_error(e_unmatchedmark);
  1088.     count -= skip + 1;
  1089.     if (count & 1)
  1090.     return_error(e_rangecheck);
  1091.     plist->u.r.read = stack_param_read;
  1092.     plist->enumerate = stack_param_enumerate;
  1093.     plist->pstack = pstack;
  1094.     plist->skip = skip;
  1095.     return ref_param_read_init(iplist, count >> 1, ppolicies, require_all, imem);
  1096. }
  1097.  
  1098. /* Implementation for putting parameters from a dictionary. */
  1099. private int
  1100. dict_param_read(iparam_list * plist, const ref * pkey, iparam_loc * ploc)
  1101. {
  1102.     ref const *spdict = &((dict_param_list *) plist)->dict;
  1103.     int code = dict_find(spdict, pkey, &ploc->pvalue);
  1104.  
  1105.     if (code != 1)
  1106.     return 1;
  1107.     ploc->presult =
  1108.     &plist->results[dict_value_index(spdict, ploc->pvalue)];
  1109.     *ploc->presult = 1;
  1110.     return 0;
  1111. }
  1112. int
  1113. dict_param_list_read(dict_param_list * plist, const ref * pdict,
  1114.              const ref * ppolicies, bool require_all,
  1115.              gs_ref_memory_t *imem)
  1116. {
  1117.     iparam_list *const iplist = (iparam_list *) plist;
  1118.     uint count;
  1119.  
  1120.     if (pdict == 0) {
  1121.     plist->u.r.read = empty_param_read;
  1122.     count = 0;
  1123.     } else {
  1124.     check_dict_read(*pdict);
  1125.     plist->u.r.read = dict_param_read;
  1126.     plist->dict = *pdict;
  1127.     count = dict_max_index(pdict) + 1;
  1128.     }
  1129.     plist->enumerate = dict_param_enumerate;
  1130.     return ref_param_read_init(iplist, count, ppolicies, require_all, imem);
  1131. }
  1132.